package com.ctshk.rpc.order.air.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.air.dto.AirOrderInvoiceInfoDTO;
import com.ctshk.rpc.order.air.entity.AirOrder;
import com.ctshk.rpc.order.air.entity.AirOrderFlight;
import com.ctshk.rpc.order.air.entity.AirOrderInvoice;
import com.ctshk.rpc.order.air.entity.AirOrderTourist;
import com.ctshk.rpc.order.air.manager.AirOrderManager;
import com.ctshk.rpc.order.air.mapper.AirOrderFlightMapper;
import com.ctshk.rpc.order.air.mapper.AirOrderInvoiceMapper;
import com.ctshk.rpc.order.air.mapper.AirOrderMapper;
import com.ctshk.rpc.order.air.mapper.AirOrderTouristMapper;
import com.ctshk.rpc.order.air.req.AirOrderInvoiceEditReq;
import com.ctshk.rpc.order.air.req.orderDetail.AirOrderFlightData;
import com.ctshk.rpc.order.air.req.orderDetail.AirOrderFlightPriceData;
import com.ctshk.rpc.order.air.service.IAirOrderInvoiceService;
import com.ctshk.rpc.system.dto.SysDepartmentDTO;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysDepartmentService;
import com.ctshk.rpc.system.service.ISysUserService;
import com.google.common.collect.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 机票订单-发票表 服务实现类
 * </p>
 *
 * @author lihang
 * @since 2021-03-29
 */
@DubboService
public class AirOrderInvoiceServiceImpl extends ServiceImpl<AirOrderInvoiceMapper, AirOrderInvoice> implements IAirOrderInvoiceService {


    @DubboReference
    private ISysUserService iSysUserService;

    @DubboReference
    private ISysDepartmentService iSysDepartmentService;

    @Autowired
    private AirOrderInvoiceMapper airOrderInvoiceMapper;

    @Autowired
    private AirOrderMapper airOrderMapper;

    @Autowired
    private AirOrderTouristMapper airOrderTouristMapper;

    @Autowired
    private AirOrderFlightMapper airOrderFlightMapper;



    @Override
    public Result<AirOrderInvoiceInfoDTO> add(Long orderId, TokenUser user) {
        /**查询订单信息*/
        AirOrder airOrder = airOrderMapper.selectById(orderId);
        /**查询订单旅客信息*/
        QueryWrapper<AirOrderTourist> orderTouristQueryWrapper =new QueryWrapper<>();
        orderTouristQueryWrapper.lambda().eq(AirOrderTourist::getOrderId,orderId);
        List<AirOrderTourist> airOrderTourists = airOrderTouristMapper.selectList(orderTouristQueryWrapper);
        StringBuffer guestBuf = new StringBuffer();
        for (AirOrderTourist airOrderTourist : airOrderTourists) {
            guestBuf.append(","+airOrderTourist.getTouristName());
        }
        UserDTO userDTO = iSysUserService.loadUserByParam(user.getId(), 1);
        SysDepartmentDTO sysDepartmentDTO =  iSysDepartmentService.queryByUserId(user.getId());
        AirOrderInvoice airOrderInvoice = new AirOrderInvoice();
        Long id = IdWorker.getId();
        airOrderInvoice.setId(id);
        airOrderInvoice.setOrderId(orderId);
        airOrderInvoice.setBranch(sysDepartmentDTO.getDeptCode()+"-"+sysDepartmentDTO.getTitle());
        airOrderInvoice.setCreateId(user.getId());
        airOrderInvoice.setGmtModified(LocalDateTime.now());
        airOrderInvoice.setIsDeleted(IsDeletedCode.NO.getCode());
        airOrderInvoice.setModifiedId(user.getId());
        airOrderInvoice.setGmtModified(LocalDateTime.now());
        airOrderInvoice.setInvoiceNo(AirOrderManager.getInvoicetNumer(sysDepartmentDTO.getDeptCode(),userDTO.getEmployeeNumber()));
        airOrderInvoice.setLicenseNo(AirOrderManager.LICENSE_NO);
        airOrderInvoice.setSupportNo(AirOrderManager.SUPPORT_NO);
        airOrderInvoice.setContactName(airOrder.getContactName());
        airOrderInvoice.setPhone(airOrder.getContactMobile());
        airOrderInvoice.setSaleName(airOrder.getSalesmanName());
        airOrderInvoice.setSalePhone(userDTO.getTelephone());
        airOrderInvoice.setGuest(guestBuf.substring(1));
        airOrderInvoice.setAmount(airOrder.getOrderAmount());
        airOrderInvoice.setPayTime(airOrder.getPayExpireDate());
        airOrderInvoiceMapper.insert(airOrderInvoice);
        return info(airOrder.getId());
    }



    @Override
    public Result<AirOrderInvoiceInfoDTO> info(Long orderId) {
        /**查询订单信息*/
        AirOrder airOrder  = airOrderMapper.selectById(orderId);
        if(null==airOrder){
            return  Result.failed(SystemError.AIR_ORDER_15000);
        }
        QueryWrapper<AirOrderInvoice> airOrderInvoiceQueryWrapper = new QueryWrapper<>();
        airOrderInvoiceQueryWrapper.lambda().eq(ObjectUtils.isNotNull(orderId),AirOrderInvoice::getOrderId,orderId);
        AirOrderInvoice airOrderInvoice = airOrderInvoiceMapper.selectOne(airOrderInvoiceQueryWrapper);
        if(null==airOrderInvoice){
            return  Result.failed(SystemError.SYS_411);
        }
        /**查询航班信息*/
        QueryWrapper<AirOrderFlight> airOrderFlightQueryWrapper = new QueryWrapper<>();
        airOrderFlightQueryWrapper.lambda().eq(AirOrderFlight::getOrderId,airOrder.getId());
        List<AirOrderFlight> airOrderFlightList  = airOrderFlightMapper.selectList(airOrderFlightQueryWrapper);
        /**组装返回结果*/
        AirOrderInvoiceInfoDTO airOrderInvoiceInfoDTO = EntityUtil.copy(airOrderInvoice,AirOrderInvoiceInfoDTO.class);
        airOrderInvoiceInfoDTO.setAmount(airOrder.getOrderAmount());
        List<AirOrderFlightData> airOrderFlightDataList = Lists.newArrayList();
        for (AirOrderFlight airOrderFlight : airOrderFlightList) {
            AirOrderFlightData airOrderFlightData = EntityUtil.copy(airOrderFlight,AirOrderFlightData.class);
            airOrderFlightData.setOrderFlightPriceDataList(JSON.parseArray(airOrderFlight.getFlightPrice(), AirOrderFlightPriceData.class));
            airOrderFlightDataList.add(airOrderFlightData);
        }
        airOrderInvoiceInfoDTO.setAirOrderFlightDataList(airOrderFlightDataList);
        return Result.success(airOrderInvoiceInfoDTO);
    }

    @Override
    public Result edit(AirOrderInvoiceEditReq req, TokenUser tokenUser){
        AirOrderInvoice airOrderInvoice = EntityUtil.copy(req,AirOrderInvoice.class);
        airOrderInvoice.setGmtModified(LocalDateTime.now());
        airOrderInvoice.setModifiedId(tokenUser.getId());
        airOrderInvoiceMapper.updateById(airOrderInvoice);
        return  Result.success(req.getId());
    }
}
